package com.atguigu.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
        HeroNode2 hero5 = new HeroNode2(7, "小李广", "花荣");
        HeroNode2 hero6 = new HeroNode2(10, "鲁智深", "花和尚");
        HeroNode2 hero7 = new HeroNode2(7, "关胜", "大刀");
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero5);
        doubleLinkedList.add(hero4);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero6);
        doubleLinkedList.list();
        System.out.println("-----------------------------");
        doubleLinkedList.update(hero7);
        doubleLinkedList.list();
        System.out.println("-----------------------------");
        doubleLinkedList.delete(7);
        doubleLinkedList.list();
        System.out.println("-----------------------------");
        HeroNode2 hero8 = new HeroNode2(11, "小明", "小明");
        doubleLinkedList.addByOrder(hero8);
        doubleLinkedList.list();
        System.out.println("-----------------------------");
        doubleLinkedList.purpleSort();
        doubleLinkedList.list();
    }
}

class DoubleLinkedList{
    private HeroNode2 head = new HeroNode2(0, "", "");


    public HeroNode2 getHead() {
        return head;
    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head;
        while (temp.next != null) {
            temp = temp.next;
            System.out.println(temp);
        }
    }

/**
 * 根据链表的编号对链表进行排序
 */


    public void purpleSort() {
        if (head == null || head.next == null) {
            return;
        }

        HeroNode2 temp = head.next;
        while (temp != null && temp.next != null) {
            HeroNode2 temp2 = temp;
            boolean flag = true;
            while (temp2 != null && temp2.next != null) {
                if (temp2.next.no < temp2.no) {
                    swapNodes(temp2, temp2.next);
                    flag = false;
                }
                temp2 = temp2.next;
            }
            if (flag) {
                break;
            }
            temp = temp.next;
        }
    }
    public void swapNodes(HeroNode2 node1, HeroNode2 node2) {
        int no = node1.no;
        String name = node1.name;
        String nickname = node1.nickname;
        node1.no = node2.no;
        node1.name = node2.name;
        node1.nickname = node2.nickname;
        node2.no = no;
        node2.name = name;
        node2.nickname = nickname;
    }

    public void add(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    /**
     * 需要在链表有序（升序）的情况下使用
     * @param heroNode：新增的节点
     */
    public void addByOrder(HeroNode2 heroNode) {
        if (head == null) {
            head = heroNode;
            return;
        }

        HeroNode2 temp = head;
        while (temp.next != null) {
            if (temp.next.no > heroNode.no) {
                break;
            }
            temp = temp.next;
        }

        if (temp.next == null && temp.no == heroNode.no) {
            System.out.printf("英雄编号%d已经存在，请换一个编号\n", heroNode.no);
        } else if (temp.no == heroNode.no) {
            System.out.printf("英雄编号%d已经存在，请换一个编号\n", heroNode.no);
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
            heroNode.pre = temp;
        }
    }

    public void update(HeroNode2 newHeronode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            temp = temp.next;
            if (temp.no == newHeronode.no) {
                flag = true;
                break;
            }
            if (temp.next == null) {
                break;
            }
        }
        if (flag) {
            temp.name = newHeronode.name;
            temp.nickname = newHeronode.nickname;
        } else {
            System.out.println("没有找到");
        }
    }

    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            if (temp.next != null){
                temp.pre.next = temp.next;
                temp.next.pre = temp.pre;
            }

            else {
                temp.pre.next = null;
            }
        } else {
            System.out.println("没有找到要删除的编号");
        }
    }
}

class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;// 指向下一个节点
    public HeroNode2 pre;// 指向前一个节点

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }


}
